<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>会员登录</title>
    <link rel="stylesheet" href="/css/bootstrap.min.css" type="text/css"/>
    <script src="/js/jquery-1.11.3.min.js" type="text/javascript"></script>
    <script src="/js/bootstrap.min.js" type="text/javascript"></script>
    <link rel="stylesheet" href="/css/common.css"/>

    <style>
        .top-margin {
            margin-top: 200px;
        }
    </style>
</head>
<body>


<div id="header">
    <!-- 头部 start -->
    <div class="top_banner">
        <img width="100%" src="/img/top_banner.jpg" alt="">
    </div>
    <div class="shortcut">
        <div class="home_page">
            <a href="/goods/list">首页</a>
        </div>
        <!-- 未登录状态  -->
        <div class="login_out">
            <span id="welcome">欢迎您！</span></span>
            <a href="/user/logout">退出</a>
            <a href="/login.html">登录</a>
            <a href="/register.html">注册</a>
        </div>
    </div>
</div>

<div class="container">
    <div class="top-margin"></div>
    <div class="row">
        <div class="col-md-4">
        </div>
        <div class="col-md-4">
            <form id="from_login" class="form-horizontal" action="login">

                <div class="form-group">
                    <label for="username" class="col-sm-3 control-label">用户名</label>
                    <div class="col-sm-6">
                        <input type="text" class="form-control" id="username" name="username" placeholder="请输入用户名">
                    </div>
                </div>
                <div class="form-group">
                    <label for="password" class="col-sm-3 control-label">密码</label>
                    <div class="col-sm-6">
                        <input type="password" class="form-control" id="password" name="password" placeholder="请输入密码">
                    </div>
                </div>

            </form>
            <div class="form-group">
                <div class="col-sm-offset-3 col-sm-12">
                    <input id="btn_login" type="submit" class="btn btn-default" style="width: 130px" value="登录"/>
                    <span id="info" style="color: #f40f0c"></span>
                </div>
            </div>
        </div>

        <div class="col-md-4">
        </div>
    </div>
</div>

<script>
    $("#btn_login").click(function () {
        // let data = $("#from_login").serialize();
        let username = $("#username").val();
        let password = $("#password").val();
        let data = {"username": username, "password": password};
        data = JSON.stringify(data);
        // alert(typeof data);
        // alert(data);
        $.ajax({
            url: "user/login",
            data: data,
            contentType: "application/json;charset=utf-8",
            method: "post",
            success: function (result) {
                // alert("成功" + result.message + ": " + result.data);
                if (result.success) {
                    $("#info").html(result.message);
                    // TODO: 跳转页面
                    location.href = "/goods/list";
                } else {
                    $("#info").html(result.message);
                }
            },
            error: function (msg) {
                alert("服务器出错: " + msg)
            }
        });
    });
</script>
</body>
</html>